<template>
  <div class="search">
    <el-input
      v-model="keyword"
      :prefix-icon="Search"
      placeholder="搜索"
      @focus="focusHandle"
      @input="changeHandle"
    ></el-input>
    <div class="add" @click="addHandle">
      <span class="iconfont icon-add"></span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { debounce } from '@renderer/utlis/utils'

defineProps({
  showAdd: {
    type: Boolean,
    default: true
  }
})
const emit = defineEmits(['addHandle', 'focusHandle', 'changeHandle'])

const keyword = ref('') // 搜索的关键字

// 点击加号
const addHandle = () => {
  emit('addHandle')
}

// 聚焦
const focusHandle = () => {
  emit('focusHandle')
}
// 搜索的值改变
const changeKeyword = debounce(() => {
  // console.log('changeHandle', keyword.value)
  emit('changeHandle', keyword.value)
}, 200)
const changeHandle = (e) => {
  keyword.value = e
  // 防抖
  changeKeyword()
}
</script>

<style lang="scss" scoped>
.search {
  height: 50px;
  display: flex;
  align-items: center;
  /* position: fixed; */
  padding: 0px 15px;
  box-sizing: border-box;
  background-color: #fff;

  .add {
    margin-left: 10px;
    padding: 6px;
    border-radius: 4px;
    background-color: #e2e2e2;
  }
}
</style>
